package com.ymatou.iapi.payment.testcase;

import java.math.BigDecimal;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.payment.parameter.AcquireOrderBean;
import com.ymatou.iapi.payment.parameter.QueryByTradingIdBean;
import com.ymatou.iapi.payment.service.QueryByTradingIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.database.model.MPayment;
import com.ymttest.database.sqlwapper.PayGateWayWapper;

/**
 * 提交退款接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_QueryByTradingId {
	private static QueryByTradingIdBean QueryByTradingIdBean;
	private static QueryByTradingIdCall QueryByTradingIdCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("提交退款接口");
	}

	@Before
	public void caseUp() {
		QueryByTradingIdBean = new QueryByTradingIdBean();
		QueryByTradingIdCall = new QueryByTradingIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(QueryByTradingIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_001() {
		Logger.start(true, "查询交易成功");
		try {
//			Boolean tempop = PayGateWayCallService.OpenYangYi;
//			PayGateWayCallService.OpenYangYi=false;
//			PayGateWayCallService.initYMatouMERID();
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("10");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
//			PayGateWayCallService.OpenYangYi=tempop;
//			if(PayGateWayCallService.OpenYangYi){
//				PayGateWayCallService.initYangYiMERID();
//			}else{
//				PayGateWayCallService.initYMatouMERID();
//			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_002() {
		Logger.start(true, "查询快速退款交易成功");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("10");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			PayGateWayCallService.fastRefund(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_003() {
		Logger.start(true, "查询第三方优惠记录成功");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("50");
			aqb.setPayPrice("6.00");
			PayGateWayCallService.acquireOrder(aqb);
			String discountAmt = "1.00";
			PayGateWayCallService.notifyCall50(aqb, discountAmt);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,discountAmt);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_004() {
		Logger.start(true, "查询交易成功，YangYi");
		try {
			Boolean tempop = PayGateWayCallService.OpenYangYi;
			PayGateWayCallService.OpenYangYi=true;
			PayGateWayCallService.initYangYiMERID();
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("11");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
			PayGateWayCallService.OpenYangYi=tempop;
			if(PayGateWayCallService.OpenYangYi){
				PayGateWayCallService.initYangYiMERID();
			}else{
				PayGateWayCallService.initYMatouMERID();
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_005() {
		Logger.start(true, "查询交易成功验证appid");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("17");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			String appid=PayGateWayCallService.WX_AppID;//17
			verifyDB(aqb,"0.00",appid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_006() {
		Logger.start(true, "查询交易成功-type6000");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("6000");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_007() {
		Logger.start(true, "查询交易成功-type2013抖音");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("2013");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_008() {
		Logger.start(true, "查询交易成功-6010盛付通微信小程序");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("6010");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_009() {
		Logger.start(true, "查询交易成功-6011盛付通微信公众号");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("6011");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_010() {
		Logger.start(true, "查询交易成功-6006代购神器HipoPayAliWap");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("6006");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_011() {
		Logger.start(true, "查询交易成功-6100GodAliPayPc");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("6100");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_012() {
		Logger.start(true, "查询交易成功-type6103");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("6103");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_013() {
		Logger.start(true, "查询交易成功-type6107");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("6107");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_014() {
		Logger.start(true, "查询交易成功-type4013");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("4013");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_015() {
		Logger.start(true, "查询交易成功-type4016");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("4016");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyDB(AcquireOrderBean aqb, String discountAmt) throws Exception {
		Logger.comment("验证数据库");
		PayGateWayWapper paymentwapper = new PayGateWayWapper();
		MPayment ppm = paymentwapper.selectPaymentByTradingid(aqb.getOrderId()).get(0);
		JsonObject result = QueryByTradingIdCall.getJsonObject().get("Result").getAsJsonObject();
		Logger.verifyEquals(result.get("PaymentId").getAsString(), ppm.getPaymentId(), "验证PaymentId");
		Logger.verifyEquals(result.get("PayType").getAsString(), ppm.getPayType().toString(), "验证PayType");
		Logger.verifyEquals(result.get("PayStatus").getAsInt(), ppm.getPayStatus(), "验证PayStatus");
		Logger.verifyEquals(result.get("MerchantId").getAsString(), PayGateWayCallService.getMerchantId(aqb.getPayType()),"验证MerchantId");
		Logger.verifyEquals(result.get("InstitutionPaymentId").getAsString(), ppm.getInstitutionPaymentId(), "验证InstitutionPaymentId");
		Logger.verifyEquals(new BigDecimal(result.get("OrderAmt").toString()).setScale(2), ppm.getOrderAmt().setScale(2), "验证PayPrice");
		Logger.verifyEquals(new BigDecimal(result.get("DiscountAmt").toString()).setScale(2),ppm.getDiscountAmt().setScale(2), "验证DiscountAmt");
		Logger.verifyEquals(new BigDecimal(discountAmt).setScale(2), ppm.getDiscountAmt().setScale(2), "验证入参DiscountAmt");
	}
	private void verifyDB(AcquireOrderBean aqb, String discountAmt,String appid) throws Exception {
		Logger.comment("验证数据库");
		PayGateWayWapper paymentwapper = new PayGateWayWapper();
		MPayment ppm = paymentwapper.selectPaymentByTradingid(aqb.getOrderId()).get(0);
		JsonObject result = QueryByTradingIdCall.getJsonObject().get("Result").getAsJsonObject();
		Logger.verifyEquals(result.get("PaymentId").getAsString(), ppm.getPaymentId(), "验证PaymentId");
		Logger.verifyEquals(result.get("PayType").getAsString(), ppm.getPayType().toString(), "验证PayType");
		Logger.verifyEquals(result.get("PayStatus").getAsInt(), ppm.getPayStatus(), "验证PayStatus");
		Logger.verifyEquals(result.get("MerchantId").getAsString(), PayGateWayCallService.getMerchantId(aqb.getPayType()),"验证MerchantId");
		Logger.verifyEquals(result.get("InstitutionPaymentId").getAsString(), ppm.getInstitutionPaymentId(), "验证InstitutionPaymentId");
		Logger.verifyEquals(new BigDecimal(result.get("OrderAmt").toString()).setScale(2), ppm.getOrderAmt().setScale(2), "验证PayPrice");
		Logger.verifyEquals(new BigDecimal(result.get("DiscountAmt").toString()).setScale(2),ppm.getDiscountAmt().setScale(2), "验证DiscountAmt");
		Logger.verifyEquals(new BigDecimal(discountAmt).setScale(2), ppm.getDiscountAmt().setScale(2), "验证入参DiscountAmt");
		Logger.verifyEquals(result.get("AppId").getAsString(),appid, "验证AppId");
	}
}
